package com.chaosj.webmagic.convert;

import com.chaosj.webmagic.dto.SearchResponseDto;
import com.chaosj.webmagic.dto.SearchEnums;
import com.chaosj.webmagic.dto.gushiwen.MingJuResponseDto;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Selectable;

/**
 * @className: MingJuConvertHandler
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/11
 **/
public class MingJuConvertHandler extends AbstractConvertPage<MingJuResponseDto>  {
    @Override
    public void convert(Page page) {
        SearchResponseDto mingJuResponseDto = createOrGet(page);
        Selectable selectable = page.getHtml().xpath("//div[@class='left']//div[@class='cont']");
        selectable.nodes().forEach(selected->{
            // 获取 名句
            String label = selected.xpath("//a/text()").nodes().get(0).get();
            String labelTarget = holdTarget(selected.xpath("//a/@href").nodes().get(0).get());
            String shuJi = selected.xpath("//a/text()").nodes().get(1).get();
            String shuJiTarget = holdTarget(selected.xpath("//a/@href").nodes().get(1).get());
            mingJuResponseDto.addObject(label, labelTarget, shuJi, shuJiTarget);
        });
    }

    @Override
    public boolean support(SearchEnums instance) {
        return SearchEnums.MING_JU.equals(instance);
    }
}
